<template>
    <div>

        <div class="app-container">
            <TopInfo />
            <Search :lookup="data.lookup" @investigate="inves" />
            <div class="w-full relative">
                <el-button type="primary" size="small" @click="add(2)">
                    <template #default>
                        <span class="text-sm">{{ $t('system.mail.addTem')}}</span>
                    </template>
                </el-button>
            </div>
        </div>
        <div class="app-container" v-loading="data.mtloading">
            <!-- 消息模版 -->
            <div>
                <el-table :data="data.list" :header-cell-style="{ 'text-align': 'center' }"
                    :cell-style="{ 'text-align': 'center' }" class="w-full">
                    <el-table-column prop="CC" :label="$t('system.mail.CC')" show-overflow-tooltip />
                    <el-table-column prop="BCC" :label="$t('system.mail.delivery')" show-overflow-tooltip />
                    <el-table-column prop="subject" :label="$t('log.title')" show-overflow-tooltip />
                    <el-table-column prop="attachment" :label="$t('system.mail.attachments')" show-overflow-tooltip />
                    <el-table-column prop="status" :label="$t('public.state')" show-overflow-tooltip width="75">
                        <template #default="scope">
                            {{ setStatus(scope.row || '--') }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="template_type" :label="$t('system.mail.type')" show-overflow-tooltip>
                        <template #default="scope">
                            {{ setsTemplate_type(scope.row || '--') }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="ao_id" :label="$t('machine.infos.organiz')" show-overflow-tooltip>
                        <template #default="scope">
                            {{ getaoid(scope.row.ao_id) }}
                        </template>
                    </el-table-column>
                    <el-table-column :label="$t('public.operate')" width="200">
                        <template #default="scope">
                            <el-button type="primary" size="small" @click="onUpdate(scope.row, 4)">{{ $t('public.modify') }}</el-button>
                            <el-button v-prmis="'email_template_del'" type="info" size="small"
                                @click="onDel(scope.row)">{{ $t('public.del')}}</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <PageNum :total="data.total" :page="data.sement" @exposeData="current" @fraction="fraction" />
        </div>
    </div>
</template>
<script lang="ts" setup>
import { useI18n } from 'vue-i18n' 
const { t: $t } = useI18n()
import TopInfo from '@/components/TopInfo/index.vue';
import Search from '@/components/Search/index.vue';
import PageNum from '@/components/PageNum/index.vue';
import { useRouter,useRoute } from 'vue-router';
import getPlevel from '@/utils/level';
import arrange from '@/utils/hierarchy';
import { getMesList, getMesdel } from '@/api/system-mail'
const router = useRouter();
const route = useRoute();
let data = reactive({
    mtloading: false, // 加载
    lookup: [// 筛选栏
        { title: $t('log.title'), type: 'input', value: '', key: 'subject' },
        { title: $t('system.mail.type'), type: 'define', select: getPlevel.emailTypeList, value: '', key: 'template_type' },
        { title: $t('machine.infos.organiz'), type: 'define', select: [], value: '', key: 'ao_id' },
    ],
    list: [], // 列表数据
    aolist: [],
    sement: { // 筛选数据
        host: '',
        username: '',
        sendEmail: '',
        pageNum: 15,
        page: 1,
        template_type: '',
        subject: '',
        ao_id: ''
    },
    total: 0, // 总条数
})
// 获取接口信息
let geturl = async () => {
    let array = await arrange.arrange(1);
    for (let item of data.lookup) {
        if (item.key == 'ao_id') {
            item.select = []
            data.aolist = []
            for (let ime of array) {
                item.select.push({
                    label: ime.ao_id + ' - ' + ime.organization_name,
                    value: ime.ao_id
                })
                data.aolist.push({
                    label: ime.organization_name,
                    value: ime.ao_id
                })
            }
        }
    }
}
geturl()
// 组织架构名称
let getaoid = (e) => {
    let name = '--'
    for (let item of data.aolist) {
        if (item.value == e) {
            name = item.label
        }
    }
    return name
}
let setsTemplate_type = (row) => {
    getPlevel.emailTypeList.forEach(item => {
        if (item.value == row.template_type) {
            row.template_typeCn = $t(item.label)
        }
    })
    return row.template_typeCn
}
let setStatus = (row) => {
    if (row.status == 1) {
        row.statusCn = $t('public.open')
    }
    if (row.status == 2) {
        row.statusCn = $t('public.disable')
    }
    return row.statusCn
}
// 获取列表信息
let getlist = () => {
    let  ec_id = route?.query.id
    data.mtloading = true
    let pram = {
        pageNum: 15,
        page: 1,
        template_type: data.sement.template_type,
        subject: data.sement.subject,
        ao_id: data.sement.ao_id,
        ec_id:ec_id
    }
    getMesList(pram).then(res => {
        data.mtloading = false
        if (res.state == 200) {
            data.list = res.data.data;
            data.total = res.data.total;
        }
    })
}
let onUpdate = (row, type) => {
    let query = {
        type: type,
        id: row.et_id
    }
    router.push({ path: '/system/mail/add', query: query });
}
// 删除
let onDel = (row: any 

) => {
    ElMessageBox.confirm($t('message.currentData'))
        .then(res => {
            let pram = {
                et_id: row.et_id
            }
            getMesdel(pram).then(res => {
                if (res.state == 200) {
                    ElMessage({
                        message: $t('message.delSuccess'),
                        type: 'success'
                    });
                    data.dialog = false
                    getlist();
                    return;
                }
                ElMessage({
                    message: $t('message.networkFailed'),
                    type: 'error'
                });
            });
        })
        .catch(err => {
            console.log('取消');
        });
};
// 查询回调
let inves = (e) => {
    for (let key in e) {
        data.sement[key] = e[key];
    }
    data.sement.page = 1;
    data.sement.pageNum = 15;
    getlist()
};

// 页数回调
let current = (e) => {
    data.sement.page = e
    getlist()
}

// 条数回调
let fraction = (e) => {
    data.sement.pageNum = e
    getlist()
}

// 广告推送
let add = (type) => {
    let query = {
        type: type,
        id: route?.query.id
    }
    router.push({ path: '/system/mail/add', query: query });
};
getlist()
</script>
